#include "test.h"

Test::Test(QWidget *parent) : QWidget(parent) {
    setupUI();          // 初始化界面
    setupConnections(); // 设置信号槽连接
}

void Test::setupUI() {
    // 主布局
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(20);

    // 视频显示区域
    videoWidget = new QVideoWidget(this);
    videoWidget->setMinimumSize(800, 480);  // 设置视频窗口最小尺寸
    videoWidget->setStyleSheet("background-color: black; border: 2px solid #ff4500; border-radius: 10px;"); // 设置黑色背景和橙色边框

    // 左侧布局（视频显示区域）
    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addWidget(videoWidget);
    leftLayout->setContentsMargins(0, 0, 0, 0);
    leftLayout->setSpacing(0);
    QWidget *leftWidget = new QWidget(this);
    leftWidget->setLayout(leftLayout);
    leftWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 右侧按钮布局
    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->setAlignment(Qt::AlignTop); // 按钮顶部对齐
    rightLayout->setSpacing(10);

    // 打开视频按钮
    openBtn = new QPushButton("打开摄像头", this);
    openBtn->setFixedHeight(60); // 增大按钮高度
    openBtn->setStyleSheet("QPushButton { font-size: 20px; background-color: #ff4500; color: white; border: none; border-radius: 10px; }"
                           "QPushButton:hover { background-color: #ff8c00; }");

    // 视频融合检测按钮
    fusionBtn = new QPushButton("视频融合检测", this);
    fusionBtn->setFixedHeight(60);
    fusionBtn->setStyleSheet("QPushButton { font-size: 20px; background-color: #ff4500; color: white; border: none; border-radius: 10px; }"
                             "QPushButton:hover { background-color: #ff8c00; }");

    // 置信度控制组件
    QHBoxLayout *confidenceLayout = new QHBoxLayout();
    confidenceLayout->setSpacing(8);

    QLabel *confidenceLabel = new QLabel("置信度:", this);
    confidenceLabel->setStyleSheet("color: white; font-size: 16px;");

    confidenceBox = new QDoubleSpinBox(this);
    confidenceBox->setRange(0.0, 1.0);
    confidenceBox->setSingleStep(0.1);
    confidenceBox->setValue(0.8);
    confidenceBox->setStyleSheet("QDoubleSpinBox {"
                                 "color: white;"
                                 "background-color: #555;"
                                 "border: 1px solid #777;"
                                 "border-radius: 4px;"
                                 "padding: 5px;"
                                 "}");

    confidenceLayout->addWidget(confidenceLabel);
    confidenceLayout->addWidget(confidenceBox);

    // 标签开关按钮
    QHBoxLayout *labelToggleLayout = new QHBoxLayout();
    labelToggleLayout->setSpacing(8);

    QLabel *toggleLabel = new QLabel("显示检测标签:", this);
    toggleLabel->setStyleSheet("color: white; font-size: 16px;");

    labelToggle = new QPushButton(this);
    labelToggle->setCheckable(true);
    labelToggle->setChecked(true); // 默认开启
    labelToggle->setFixedSize(60, 30);
    labelToggle->setStyleSheet(
        "QPushButton {"
        "   border: 2px solid #777;"
        "   border-radius: 15px;"
        "   background-color: #ff4500;" // 开启状态颜色
        "}"
        "QPushButton:checked {"
        "   background-color: #ff4500;" // 开启状态颜色
        "}"
        "QPushButton:!checked {"
        "   background-color: #555;"    // 关闭状态颜色
        "}"
        );

    labelToggleLayout->addWidget(toggleLabel);
    labelToggleLayout->addWidget(labelToggle);

    // 添加所有组件到右侧布局
    rightLayout->addWidget(openBtn);
    rightLayout->addWidget(fusionBtn);
    rightLayout->addLayout(confidenceLayout);
    rightLayout->addLayout(labelToggleLayout);

    // 右侧布局容器
    QWidget *rightWidget = new QWidget(this);
    rightWidget->setLayout(rightLayout);
    rightWidget->setFixedWidth(250); // 增大右侧宽度
    rightWidget->setStyleSheet("background-color: #333333; border-radius: 10px; padding: 10px;");

    // 添加左侧和右侧到主布局
    mainLayout->addWidget(leftWidget, 4); // 左侧占4份
    mainLayout->addWidget(rightWidget, 1); // 右侧占1份

    // 媒体播放器
    player = new QMediaPlayer(this);
    player->setVideoOutput(videoWidget); // 设置视频输出到 videoWidget

    // 设置窗口属性
    this->setLayout(mainLayout);
    this->setWindowTitle("烟幕猎手");
    this->resize(1200, 720); // 增大窗口初始大小
}

void Test::setupConnections() {
    // 连接按钮点击信号到槽函数
    connect(openBtn, &QPushButton::clicked, this, &Test::openVideoFile);
    connect(fusionBtn, &QPushButton::clicked, this, &Test::playVideo);
    // 连接标签开关按钮
    connect(labelToggle, &QPushButton::toggled, this, [this](bool checked) {
        // 这里可以添加标签显示/隐藏的逻辑
        qDebug() << "标签显示状态:" << (checked ? "显示" : "隐藏");
    });
    // 连接置信度变化信号
    connect(confidenceBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
            this, [this](double value) {
                qDebug() << "置信度设置为:" << value;
            });
}

void Test::openVideoFile() {
    // 打开文件对话框，选择视频文件
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("打开视频"),
                                                    QDir::homePath(),
                                                    tr("视频文件 (*.mp4 *.avi *.mkv *.mov)"));

    if (!fileName.isEmpty()) {
        player->setSource(QUrl::fromLocalFile(fileName)); // 设置视频源
        player->play(); // 开始播放
    }
}

void Test::playVideo() {
    // 摄像头捕获（被按钮调用）
    if (player->playbackState() == QMediaPlayer::StoppedState) {
        player->play();
    }
}
